package org.goj.user.controller;


import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.goj.common.constant.ResultCode;
import org.goj.common.entity.user.Topic;
import org.goj.common.model.Result;
import org.goj.feign.feign.BaseFeignClient;
import org.goj.user.service.TopicService;
import org.goj.user.vo.SevenRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 论坛帖子表 前端控制器
 * </p>
 *
 * @author CK
 * @since 2020-12-02
 */
@RestController
@RequestMapping("/topic")
public class TopicController {

    @Resource
    private TopicService topicService;

    @Resource
    private BaseFeignClient baseFeignClient;

    /**
     * 查询轮播信息最新五条
     *
     * @return Result<Page < Topic>>
     * @author CK
     * @date 2021/4/9 16:59
     */
    @GetMapping("/listTopic")
    public Result<Page<Topic>> listTopic() {
        Page<Topic> page = topicService.listTopic();
        return Result.succeed("查询轮播信息成功", page);
    }

    /**
     * 管理员查询轮播信息
     *
     * @param topicId 轮播ID
     * @return Result<List < Topic>>
     * @author CK
     * @date 2021/4/10 17:41
     */
    @GetMapping("/adminListTopic")
    public Result<List<Topic>> adminListTopic(
            @RequestParam(value = "topicId", required = false, defaultValue = "-1") Integer topicId) {
        List<Topic> list = topicService.adminListTopic(topicId);
        if (topicId != -1) {
            Assert.isTrue(list.size() == 1, "查询轮播信息失败");
        }
        return Result.succeed("查询轮播信息成功", list);
    }

    /**
     * 管理员删除轮播信息
     *
     * @param topicId 轮播信息ID
     * @return Result<Boolean>
     * @author CK
     * @date 2021/4/10 18:03
     */
    @PostMapping("/deleteTopic")
    public Result<Boolean> deleteTopic(Integer topicId) {
        boolean flag = topicService.removeById(topicId);
        Assert.isTrue(flag, "删除轮播图失败");
        return Result.succeed("删除轮播图成功", flag);
    }

    /**
     * 插入或修改一条轮播记录
     *
     * @param topicId     轮播图ID
     * @param description 描述
     * @param status      状态
     * @param link        链接
     * @param target      是否外链
     * @return Result<Boolean>
     * @author CK
     * @date 2021/4/9 17:23
     */
    @PostMapping("insertTopic")
    public Result<Boolean> insertTopic(
            @RequestParam(value = "topicId", required = false, defaultValue = "-1") Integer topicId,
            @RequestParam(value = "description") String description,
            @RequestParam(value = "status", required = false, defaultValue = "0") Boolean status,
            @RequestParam(value = "link", required = false, defaultValue = "/home") String link,
            @RequestParam(value = "target", required = false, defaultValue = "0") Boolean target,
            @RequestPart(value = "src") MultipartFile src) {
        Result<String> url = baseFeignClient.upload(src);
        Assert.isTrue(url.getCode().equals(ResultCode.SUCCESS.getCode()), "添加轮播图失败");
        Topic topic = new Topic();
        String formerUrl = "";
        if (topicId != -1) {
            topic.setTopicId(topicId);
            formerUrl = topicService.getById(topicId).getSrc();
        }
        topic.setInDate(new Date()).setDescription(description).setStatus(status ? 1 : 0)
                .setLink(link).setTarget(target ? 1 : 0).setSrc(url.getData());
        boolean flag = topicService.saveOrUpdate(topic);
        Assert.isTrue(flag, topicId == -1 ? "插入轮播图信息失败" : "更新轮播图信息失败");
        if (!"".equals(formerUrl)) {
            baseFeignClient.delete(formerUrl);
        }
        return Result.succeed(topicId == -1 ? "插入轮播图信息成功" : "更新轮播图信息成功", true);
    }

    /**
     * 查询过去七日记录
     *
     * @return Result<List < SevenRecordVO>>
     * @author CK
     * @date 2021/4/10 10:25
     */
    @GetMapping("/sevenRecord")
    public Result<List<SevenRecordVO>> sevenRecord() {
        List<SevenRecordVO> list = topicService.sevenRecord();
        Assert.isTrue(list.size() == 7, "查询七日记录失败");
        return Result.succeed("查询七日记录成功", list);
    }

}

